/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.meraklisina.latexeditor;

import com.meraklisina.latexeditor.mdi.Common.Tools;
import com.meraklisina.latexeditor.mdi.DialogActions.ActionOpen;
import com.meraklisina.latexeditor.mdi.TextEditor.TextEditors;
import com.meraklisina.latexeditor.toolbarCustomize.CustomizeForm;
import com.meraklisina.latexeditor.mdi.DialogActions.ActionSave;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;

/**Thread class for autosave feature
 *
 * @author eozkanal
 */
public class AutoSaveThread extends java.lang.Thread{
    private Integer AutoSaveInterval;
	private ExeditorLatexEditor editor;
	private Boolean AutoSave;
    private ExeditorLatexEditor getEditor()
    {
        return this.editor;
    }

    public AutoSaveThread(ExeditorLatexEditor editor)
    {
        this.editor=editor;
        
        AutoSave AutoSaveInt = new AutoSave(); //Ali
        AutoSaveInterval = Integer.parseInt(AutoSaveInt.getAutoSaveInterval());
        AutoSave = AutoSaveInt.getAutoSaveClick();
        if (AutoSaveInterval == 0 || AutoSaveInterval == null) {
        	AutoSaveInt.setAutoSaveFalse();
        	AutoSave = AutoSaveInt.getAutoSaveClick();
        }//Ali
    }

    /** run method for thread calls.
     * Iterates over tabs and saves tab which are saved previously(have a file name) and has changes at every 5 minutes.
     */
    public void run()
    {
        ActionSave ClsActionSave = new ActionSave();

        if (AutoSave) {
	        while(true)
	        {
	            try
	            {
	                java.lang.Thread.sleep(60000*AutoSaveInterval);
	            }
	            catch(InterruptedException ex)
	            {
	
	            }
	
	            TextEditors ClsTextEditor = new TextEditors();
	
	            for(int i=0; i<ClsTextEditor.getEditors().size(); i++)
	            {
	                JTextPane tp=ClsTextEditor.GetJTextPaneAt(this.getEditor().tab_Editors, i);
	
	                String filePath=ClsTextEditor.GetRealPath(i);
	
	                if(!filePath.isEmpty() && ClsTextEditor.GetIsTextChanged(i))
	                {
	                    ClsActionSave.Save(this.getEditor().tab_Editors, i, filePath, false);
	                }
	            }
	        }
        }
    }
}